package com.future.bantang.utils;

import android.text.TextUtils;

import com.future.bantang.app.BanTangAPP;
import com.future.bantang.constants.URL;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.util.Map;

/**
 * Created by Administrator on 16-1-11.
 */
public class NetUtils {
    private static HttpUtils mHttpUtils;

    static {
        mHttpUtils = BanTangAPP.getApp().getHttpUtils();
    }

    /**
     * 生成网络访问参数的方法，链式编程
     */
    public static class RequestParamsBuilder {
        private RequestParams mParams;

        public RequestParamsBuilder() {
            mParams = new RequestParams();
        }

        public RequestParamsBuilder addPostParameter(String key, String value) {
            if (!TextUtils.isEmpty(key) && !TextUtils.isEmpty(value)) {
                mParams.addBodyParameter(key, value);
            }
            return this;
        }

        public RequestParamsBuilder addGetParameter(String key, String value) {
            if (!TextUtils.isEmpty(key) && !TextUtils.isEmpty(value)) {
                mParams.addQueryStringParameter(key, value);
            }
            return this;
        }

        public RequestParams build() {
            return mParams;
        }
    }


    /**
     * Post方法访问网络的方法；
     *
     * @param url
     * @param params
     * @param callBack
     */
    public static void doPost(String url, RequestParams params, RequestCallBack<String> callBack) {
        mHttpUtils.send(HttpRequest.HttpMethod.POST, url, params, callBack);
    }

    /**
     * work by liufujie
     * 获得半塘广场数据
     *
     * @param params
     * @param callBack
     */
    public static void getSquare(RequestParams params, RequestCallBack<String> callBack) {
        doPost(URL.SQUARE_URL, params, callBack);
    }

    /**
     * work by liufujie
     * 获得热门推荐数据
     *
     * @param isDoubleTag 动态改变网址
     * @param params
     * @param callBack
     */
    public static void getSquareRecommenedHotDetail(boolean isDoubleTag, RequestParams params, RequestCallBack<String> callBack) {
        if (isDoubleTag){
            doPost(URL.SQUARE_RECOMMENED_HOT_TAG_URL, params, callBack);
        }
        doPost(URL.SQUARE_RECOMMENED_HOT_URL, params, callBack);
    }

    /**
     * 获取主fragment的数据
     *
     * @param params
     * @param callBack
     */
    public static void loadMainDatas(RequestParams params, RequestCallBack<String> callBack) {
        doPost(URL.HOME_URL, params, callBack);
    }

    /**
     * 获取分栏的数据
     *
     * @param params
     * @param callBack
     */
    public static void loadSubFieldDatas(RequestParams params, RequestCallBack<String> callBack) {
        doPost(URL.SUBFIELD_URL, params, callBack);
    }


    /**
     * 获取ShoppingList的数据
     *
     * @param params
     * @param callBack
     */
    public static void loadShoppingListDatas(RequestParams params, RequestCallBack<String> callBack) {
        doPost(URL.SHOPPING_LIST_URL, params, callBack);
    }


    /**
     * 获取“用户推荐”的数据
     * @param params
     * @param callBack
     */
    public static void loadUserRecommendDatas(RequestParams params,RequestCallBack<String> callBack){
        doPost(URL.USER_RECOMMEND_URL, params, callBack);
    }










    /**
     * Post方法访问网络的方法；
     *
     * @param url
     * @param map
     * @param callBack
     */
    @Deprecated
    public static void doPost(String url, Map<String, String> map, RequestCallBack<String> callBack) {
        RequestParams params = new RequestParams("utf-8");
        if (map != null) {
            for (Map.Entry<String, String> entry : map.entrySet()) {
                if (entry != null && !TextUtils.isEmpty(entry.getKey())) {
                    params.addBodyParameter(entry.getKey(), entry.getValue());
                }
            }
        }
        mHttpUtils.send(HttpRequest.HttpMethod.POST, url, params, callBack);
    }
}
